package fr.cephb.joperon.structures.bindings;

import com.sleepycat.bind.tuple.TupleInput;
import com.sleepycat.bind.tuple.TupleOutput;

import fr.cephb.berkeley.baseapi.bindings.AbstractTupleBinding;
import fr.cephb.joperon.structures.STypes;

public class STypesArrayBinding
	extends AbstractTupleBinding<STypes[]>
	{
	private int size;
	public STypesArrayBinding(int size)
		{
		this.size=size;
		}
	
	public int getArraySize()
		{
		return this.size;
		}
	
	@Override
	public STypes[] entryToObject(TupleInput in)
		{
		STypes array[]=new STypes[getArraySize()];
		for( int i=0;i< array.length;++i)
			{
			STypes type=new STypes();
			array[i]=type;
			type.a= in.readUnsignedByte();
			type.c= in.readUnsignedByte();
			type.g= in.readUnsignedByte();
			type.t= in.readUnsignedByte();
			
			type.n= in.readUnsignedByte();
			
			type.d= in.readUnsignedByte();
			type.i= in.readUnsignedByte();
			type.f= in.readUnsignedByte();
			}
		return array;
		}

	@Override
	public void objectToEntry(STypes stypes[], TupleOutput out)
		{
		if(stypes.length!=getArraySize()) throw new IllegalArgumentException("length:"+stypes.length);
		for(STypes type:stypes)
			{
			out.writeUnsignedByte(type.a);
			out.writeUnsignedByte(type.c);
			out.writeUnsignedByte(type.g);
			out.writeUnsignedByte(type.t);
			
			out.writeUnsignedByte(type.n);
			
			out.writeUnsignedByte(type.d);
			out.writeUnsignedByte(type.i);
			out.writeUnsignedByte(type.f);
			}
		}

	}
